package caculate.stack;

import java.util.Stack;

/**
 * @program: rocsun
 * @description:
 * @author: yx.guo
 * @create: 2025-04-10 17:12
 **/
public class IsValid {

    public boolean isValid(String s) {
        int len = s.length();

        Stack<Character> stack = new Stack<>();

        for (int i = 0; i < len; i++) {
            // 如果stack.pop()是一个左括号， s.charAt(i) 是一个右括号，那么stack.
            if (stack.isEmpty() || parenthesesMatch(s.charAt(i)) != stack.peek()) {
                //  放入
                stack.push(s.charAt(i));
            } else {
                stack.pop();
            }
        }

        return stack.isEmpty();
    }

    public static char parenthesesMatch(char l) {
         switch (l) {
            case ')':
                return '(';
            case ']':
                return '[';
            case '}':
                return '{';
             default:
                 return ' ';
        }
    }

}
